<script setup>
import { ref } from 'vue'
import { ElMessageBox } from 'element-plus'
import {getCourseResourceListService,
  insertCourseResourceListService,
  updCourseResourceListService
} from '@/api/CourseResource.js'
import { UploadFilled } from '@element-plus/icons-vue'
const total = ref(0)
const params = ref({
  courseName:'',
  courseSubName:'',
  pageNum:1,
  pageSize:5
})

const courseResourceList = ref([])
const getCourseResourceList = async () => {
  const res = await getCourseResourceListService(params.value)
  courseResourceList.value = res.data.data
  total.value = res.data.total
}
getCourseResourceList()


const handleCurrentChange = (page) => {
  params.value.pageNum = page  
  getCourseResourceList()
}



const editInsertBtn = () => {
  showDrawer.value = !showDrawer.value
}

const showDrawer = ref(false)

const courseResourceVo = ref({
  id:'',
  courseId:'',
  courseSubId: '',
  courseName: '',
  courseSubName: '',
  resourceName: '',
  resourcePath: '',
  resourceType: '',
  resourceTypeDesc: '',
})
const courseResourceVoRef=ref()

const confirmClick = async ()=>{
  await courseResourceVoRef.value.validate()
  if(courseResourceVo.value.id){
    await updCourseResourceListService(courseResourceVo.value)
  }else{
    await insertCourseResourceListService(courseResourceVo.value)
  }
  
  ElMessage.success('成功！')
  courseResourceVo.value=({})
  showDrawer.value =  false
  fileList.value = []
  getCourseResourceList()
}

const fileList = ref([])

const uploadFile = (response,uploadFile) => {
  fileList.value = []
  const newFileItem = { name: uploadFile.name, url: response };  
  fileList.value.push(newFileItem);
  courseResourceVo.value.resourceName = uploadFile.name
  courseResourceVo.value.resourcePath = response
  console.log(courseResourceVo.value)
}

const rules = {
  courseId:[{required:true,message:'请输入课程编号',trigger:'blur'}],
  courseSubId:[
        {required:true,message:'请输入课节编号',trigger:'blur'},
    ],
    resourceName:[
        {required:true,message:'请等待资源上传',trigger:'blur'},
    ],
    resourcePath:[
        {required:true,message:'请等待资源上传',trigger:'blur'},
    ],
    resourceType:[
        {required:true,message:'请选择资源类型',trigger:'blur'},
    ],
}


// 编辑
const editResourceBtn = (row) => {
  console.log(row)
  showDrawer.value = !showDrawer.value
    // 只更新需要的属性，而不是替换整个对象  
  courseResourceVo.value.id = row.id;  
  courseResourceVo.value.courseId = row.courseId;  
  courseResourceVo.value.courseSubId = row.courseSubId;  
  courseResourceVo.value.courseName = row.courseName;  
  courseResourceVo.value.courseSubName = row.courseSubName;  
  courseResourceVo.value.resourceName = row.resourceName;  
  courseResourceVo.value.resourcePath = row.resourcePath;  
  // 确保resourceType是字符串  
  courseResourceVo.value.resourceType = row.resourceType.toString();  
  courseResourceVo.value.resourceTypeDesc = row.resourceTypeDesc;  
  fileList.value = []
  const newFileItem = { name: row.resourceName, url: row.resourcePath };  
  fileList.value.push(newFileItem);
}

const search = ()=> {
  getCourseResourceList()
}
</script>

<template>
  <div>
    <div class="cus-header">
        <el-card>
          <div style="display: flex;justify-content: space-between;">
            <div class="search">
                <div>
                    课程：
                    <el-input v-model="params.courseName" style="width: 240px" placeholder="请输入课程名称" />
                </div>
                <div style="margin-left: 20px;">
                    课节：
                    <el-input v-model="params.courseSubName" style="width: 240px" placeholder="请输入课节" />
                </div>
                <div style="margin-left: 20px;">
                    <el-button @click="search" style="width: 80px;" type="primary">搜索</el-button>
                </div>
            </div>
            <div class="insert">
              <el-button style="width: 80px;" type="primary" @click="editInsertBtn">新增</el-button>
            </div>
          </div>
            
            
        </el-card>
    </div>
    <div class="cus-body">
        <el-card style="min-height: 650px;">
            <el-table :data="courseResourceList" style="width: 100%" height="400" >
                <el-table-column fixed prop="courseId" label="课程编号" width="150" />
                <el-table-column prop="courseName" label="课程名称" width="120" />
                <el-table-column prop="courseSubId" label="课节编号" width="120" />
                <el-table-column prop="courseSubName" label="课节名称" width="120" />
                <el-table-column prop="resourceTypeDesc" label="资源类型" width="600" />
                <el-table-column prop="resourceName" label="资源名称" width="120" />
                <el-table-column fixed="right" label="操作" min-width="120">
                <template #default="{row}">
                    <el-button link type="primary" size="small" @click="editResourceBtn(row)">
                        编辑
                    </el-button>
                    <el-button link type="primary" size="small">删除</el-button>
                </template>
                </el-table-column>
            </el-table>

            <!-- 分页 -->
             <div style="margin-top: 20px;">
              <el-pagination
              v-model:current-page="params.pageNum"
              v-model:page-size="params.pageSize"
              :background="true"
              layout="jumper,prev, pager, next,total"
              :total=total
              @current-change="handleCurrentChange"
              style="justify-content: end;"
            />
             </div>
            
        </el-card>
    </div>
  </div>
  <!-- 新增或编辑 -->
  <el-drawer v-model="showDrawer" >
    <template #header>
      <h4>操作</h4>
    </template>
    <template #default>
      <div>
        <el-form
          :model="courseResourceVo" 
          ref="courseResourceVoRef"
          :rules="rules"
          label-width="auto">
          <el-form-item label="课程编号" :label-position="left" prop="courseId">
            <el-input v-model="courseResourceVo.courseId" />
          </el-form-item>
          <el-form-item label="课节编号" :label-position="left" prop="courseSubId">
            <el-input v-model="courseResourceVo.courseSubId" />
          </el-form-item>
          <el-form-item label="资源类型" :label-position="left" prop="resourceType">
            <el-select v-model="courseResourceVo.resourceType" placeholder="请选择">
              <el-option label="文档" value='1' />
              <el-option label="PDF" value='2' />
              <el-option label="视频" value='3' />
            </el-select>
          </el-form-item>
            <el-upload
              v-model:file-list="fileList"
              action="http://localhost:8080/api/file/upload"
              multiple
              drag
              :on-success="uploadFile"  
            >
              <el-icon class="el-icon--upload"><upload-filled /></el-icon>
              <div class="el-upload__text">
                <em>点击上传</em>
              </div>
            </el-upload>
        </el-form>
      </div>
    </template>
    <template #footer>
      <div style="flex: auto">
        <el-button @click="editInsertBtn">取消</el-button>
        <el-button type="primary" @click="confirmClick">提交</el-button>
      </div>
    </template>
  </el-drawer>
</template>


<style lang="scss" scoped>
    .flex-row{
        display: flex;
        flex-direction: row;
    }
    .cus-header{
        .search{
            display: flex;
            flex-direction: row;
        }
    }
    .cus-body{
        margin-top: 15px;
    }
</style>